package com.fyp.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fyp.database.DBInsert;

/**
 * Servlet implementation class for Servlet: RequestResponse
 * 
 */
public class RequestResponse extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public RequestResponse() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	public String computerName() throws UnknownHostException {
		return InetAddress.getLocalHost().getHostName();
	}
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		int count = 0;
		String ConnectionURL = "jdbc:derby:/DerbyDB/AssetDB";
		Connection con = null;
		ResultSet rs;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		count++;
		String title = request.getParameter("formtext1");
		String description = request.getParameter("textarea1");
		
		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			con = DriverManager.getConnection(ConnectionURL);
			String sql = "insert into requests values (?,?,?,?)";
		    PreparedStatement pst = con.prepareStatement(sql);
		    pst.setInt(1, count);
		    pst.setString(2, computerName());
		    pst.setString(3, title);
		    pst.setString(4, description);
		    int numRowsChanged = pst.executeUpdate();
		    
		    out.println("<HTML><BODY>");
		    out.println("<H2>Thank you for submitting your query.\n");
		    out.println("The title you entered is as follows:</H2>\n");
		    out.println(title);
		    out.println("\n<H2>The description you entered is as follows:</H2>\n");
		    out.println(description);
		    out.println("</BODY></HTML>");
		    
//		    out.println("Thank you for submitting your query.\n");
//		    out.println("The title entered is as follows: ");
//		    out.println(title + "\n");
//		    out.println("The description entered is as follows: ");
//		    out.println(description + "\n");
		    pst.close();
		} catch(ClassNotFoundException e){
		      out.println("Couldn't load database driver: " + e.getMessage());
	    }
	    catch(SQLException e){
	      out.println("SQLException caught: " + e.getMessage());
	    }
	    catch (Exception e){
	      out.println(e);
	    }
	    finally {
	      // Always close the database connection.
	      try {
	        if (con != null) con.close();
	      }
	      catch (SQLException ignored){
	        out.println(ignored);
	      }
	    }
	}
}